package com.zhuj.core;

import com.zhuj.client.NrpcClient;
import com.zhuj.core.annotation.NrpcService;
import com.zhuj.core.util.AssistUtil;
import com.zhuj.core.util.JedisUtil;
import com.zhuj.core.util.StringUtil;
import com.zhuj.register.Register;
import com.zhuj.server.NrpcServer;
import com.zhuj.server.ServiceRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import static com.sun.org.apache.xerces.internal.utils.SecuritySupport.getResourceAsStream;

/**
 * Created by Administrator on 2018/1/3.
 */
public class InitNrpc {

    private static final Logger LOG = LoggerFactory.getLogger(InitNrpc.class);

    private static String app;

    private static String address;

    private static int port;

    private static String codec;

    private static String[] scanPackages;

    public static ServiceRegistry serviceRegistry = ServiceRegistry.getInstance();

    private static String redisAddress;

    private static int redisPort;

    private static NrpcClient client;

    public void initConfig() {
        try {
            initProperty();
            startService();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void startService() throws Exception {
    	if (redisAddress != null) JedisUtil.init();
        if("provider".equals(app)){
            final NrpcServer server = new NrpcServer();
            new Thread(){
                @Override
                public void run() {
                    server.start(port);
                }
            }.start();
        }else if("consumer".equals(app)){
            client = new NrpcClient();
            new Thread(NrpcClient.processRunnable).start();
        }
    }

    private void initProperty() throws Exception {
        Properties pro = new Properties();
        InputStream in = null;
        //加载配置文件，初始化固定信息
        try {
            in = getResourceAsStream("nrpc.properties");
            pro.load(in);
        } catch (FileNotFoundException e) {
            LOG.info("没有nrpc.properties文件");
            return;
        } catch (IOException e) {
            LOG.info("配置文件加载出错");
            return;
        }
        app = pro.getProperty("app");
        if("provider".equals(app)){
            //扫描指定包，初始化服务接口
            if (!"spring".equals(pro.getProperty("serviceProvider"))){
                serviceRegistry.registryByNative(pro.getProperty("package"));
                if (serviceRegistry.isEmpty()){
                    LOG.error("没有可初始化的服务！");
                    throw new Exception("没有可初始化的服务！");
                }
            }
            port = Integer.parseInt(pro.getProperty("port"));
            String regurl = pro.getProperty("registerUrl");
            if (regurl != null){
                String[] addpt = regurl.split(":");
                redisAddress = addpt[0];
                redisPort = Integer.valueOf(addpt[1]);
            }
        }else if ("consumer".equals(app)){
            address = pro.getProperty("address");
            //单点配置优先
            if (address == null){
                String[] regurl = pro.getProperty("registerUrl").split(":");
                redisAddress = regurl[0];
                redisPort = Integer.valueOf(regurl[1]);
            }else {
            	port = Integer.parseInt(pro.getProperty("port"));
			}
        }
        codec = pro.getProperty("codec");
    }

    public void registryService(Object[] beans){
        try {
            serviceRegistry.registryBySpring(beans);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getAddress() {
        return address;
    }

    public static int getPort() {
        return port;
    }

    public static String getCodec() {
        return codec;
    }

    public static String getRedisAddress() {
        return redisAddress;
    }

    public static int getRedisPort() {
        return redisPort;
    }

    public static NrpcClient getClient() {
        return client;
    }
}
